

SELECT     hanghoa.maHang, sum(nhapKhoDetail.soLuong)AS nhap 
FROM         hanghoa left JOIN
                      nhapKhoDetail ON hanghoa.maHang = nhapKhoDetail.maHang 
Group by hanghoa.maHang


SELECT     hanghoa.maHang,sum( xuatKhoDetail.soLuong) AS xuat
FROM         hanghoa left JOIN
                      xuatKhoDetail ON hanghoa.maHang = xuatKhoDetail.maHang
Group by (hanghoa.maHang)


SELECT   count(nhapKhoDetail.mahang) as c,  hanghoa.maHang, sum(nhapKhoDetail.soLuong)AS nhap,sum( xuatKhoDetail.soLuong) AS xuat
FROM         hanghoa left JOIN
                      nhapKhoDetail ON hanghoa.maHang = nhapKhoDetail.maHang left JOIN
                      xuatKhoDetail ON hanghoa.maHang = nhapKhoDetail.mahang
Group by hanghoa.maHang , nhapKhoDetail.mahang ,nhapKhoDetail.mahang



SELECT     hanghoa.maHang, nhapKhoDetail.soLuong AS nhap,xuatKhoDetail.soLuong AS xuat
FROM         hanghoa left JOIN
                      nhapKhoDetail ON hanghoa.maHang = nhapKhoDetail.maHang left JOIN
                      xuatKhoDetail ON hanghoa.maHang = xuatKhoDetail.maHang 
Group by hanghoa.maHang ,nhapKhoDetail.soLuong,xuatKhoDetail.soLuong

DECLARE @xc int;
DECLARE @nc int;
SELECT    @xc=count(xuatKhoDetail.maHang ) ,@nc= count(nhapKhoDetail.maHang ) , hanghoa.maHang, sum(nhapKhoDetail.soLuong)/@xc AS nhap,sum(xuatKhoDetail.soLuong)/@nc AS xuat
FROM         hanghoa left JOIN
                      nhapKhoDetail ON hanghoa.maHang = nhapKhoDetail.maHang left JOIN
                      xuatKhoDetail ON hanghoa.maHang = xuatKhoDetail.maHang 
Group by hanghoa.maHang ,nhapKhoDetail.soLuong,xuatKhoDetail.soLuong



SELECT     hanghoa.maHang, nhapKhoDetail.soLuong AS nhap,xuatKhoDetail.soLuong AS xuat
FROM         hanghoa left JOIN
                      nhapKhoDetail ON hanghoa.maHang = nhapKhoDetail.maHang left JOIN
                      xuatKhoDetail ON hanghoa.maHang = xuatKhoDetail.maHang 
Group by hanghoa.maHang ,nhapKhoDetail.soLuong,xuatKhoDetail.soLuong